<!--
 * @Descripttion: 群发短信
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2022-12-08 11:39:28
-->
<template>
    <div class="lb-group-news">
      <top-nav></top-nav>
      <div class="page-main">
        <el-form
          @submit.native.prevent
          :model="subForm"
          :rules="subFormRules"
          ref="subForm"
          label-width="160px"
        >
          <!-- <el-form-item label="签名" prop="short_sign">
            <el-input
              v-model="subForm.short_sign"
              placeholder="请输入签名"
            ></el-input>
            <lb-tool-tips>签名在阿里云短信签名管理处查看</lb-tool-tips>
          </el-form-item> -->
          <el-form-item label="下单模板" prop="hx_order_short_code">
            <el-input
              v-model="subForm.hx_order_short_code"
              placeholder="请输入下单模板"
            ></el-input>
            <lb-tool-tips>短信模板, 如: SMS_129755997</lb-tool-tips>
          </el-form-item>
          <el-form-item label="退单模板" prop="hx_refund_short_code">
            <el-input
              v-model="subForm.hx_refund_short_code"
              placeholder="请输入下单模板"
            ></el-input>
            <lb-tool-tips>短信模板, 如: SMS_129755997</lb-tool-tips>
          </el-form-item>
          <el-form-item label="求救通知模板" prop="hx_help_short_code">
            <el-input
              v-model="subForm.hx_help_short_code"
              placeholder="请输入求救通知模板"
            ></el-input>
            <lb-tool-tips>短信模板, 如: SMS_129755997</lb-tool-tips>
          </el-form-item>
          <el-form-item label="是否启用亨讯短信" prop="hx_status">
            <el-radio-group v-model="subForm.hx_status">
              <el-radio :label="1">{{ $t('action.ON') }}</el-radio>
              <el-radio :label="0">{{ $t('action.OFF') }}</el-radio>
            </el-radio-group>
            <lb-tool-tips
              >开启后，必须配置短信模板,
              用于手机端；下单时若用户没有授权手机号，则通过短信验证码收集手机号码</lb-tool-tips
            >
          </el-form-item>
          <el-form-item
            label="短信模板"
            prop="hx_short_code"
            v-if="subForm.hx_status === 1"
          >
            <el-input
              v-model="subForm.hx_short_code"
              placeholder="请输入短信模板"
            ></el-input>
            <lb-tool-tips
              >短信模板, 如: SMS_129755997，用于发送短信验证码</lb-tool-tips
            >
          </el-form-item>
          <el-form-item>
            <lb-button type="primary" @click="submitFormInfo('subForm')">{{
              $t('action.submit')
            }}</lb-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    data () {
      let checkShortCode = (rule, value, callback) => {
        if (this.subForm.hx_status === 1 && !this.subForm.hx_short_code) {
          callback(new Error('请输入短信模板'))
        } else {
          callback()
        }
      }
      return {
        subForm: {
        //   short_sign: '',
          hx_order_short_code: '',
          hx_refund_short_code: '',
          hx_help_short_code: '',
          hx_status: 0,
          hx_short_code: ''
        },
        subFormRules: {
        //   short_sign: { required: true, validator: this.$reg.isNotNull, text: '签名', reg_type: 2, trigger: 'blur' },
          hx_order_short_code: { required: true, validator: this.$reg.isNotNull, text: '下单模板', reg_type: 2, trigger: 'blur' },
          hx_refund_short_code: { required: true, validator: this.$reg.isNotNull, text: '退单模板', reg_type: 2, trigger: 'blur' },
          hx_help_short_code: { required: true, validator: this.$reg.isNotNull, text: '求救通知模板', reg_type: 2, trigger: 'blur' },
          hx_status: { required: true, type: 'number', message: '请选择是否启用短信模板', trigger: 'blur' },
          hx_short_code: { required: true, validator: checkShortCode, trigger: 'blur' }
        }
      }
    },
    async created () {
      await this.getDetail()
    },
    methods: {
      async getDetail () {
        let { code, data } = await this.$api.system.configInfo()
        if (code !== 200) return
        for (let key in this.subForm) {
          this.subForm[key] = data[key]
        }
      },
      submitFormInfo (name) {
        this.$refs[name].validate(valid => {
          if (valid) {
            let subForm = JSON.parse(JSON.stringify(this.subForm))
            this.$api.system.configUpdate(subForm).then(res => {
              if (res.code === 200) {
                this.$message.success(this.$t('tips.successSub'))
              }
            })
          }
        })
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .lb-group-news {
    width: 100%;
    .page-main {
      .el-input {
        width: 300px;
      }
    }
  }
  </style>
  